

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelUtil  {
	
	private static Log log=LogFactory.getLog(ExcelUtil.class);
	/**
	 * 导出excel
	 * @param headerList 列头描述文字
	 * @param list 表格行数据
	 * @param path 文件相对路径
	 * @coder 向问天
	 * @author 上海托科软件有限公司 2009-6-9
	 */
	public static void exportExcel(String[] key,String[] desc,List list,String path) throws Exception {
		POIFSFileSystem fs;
		FileOutputStream fOut = null;
		String fileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss")
		.format(new Date()) + ".xls";
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("Sheet1");
			
			HSSFRow keyRow = sheet.createRow((short) 0);
			HSSFRow descRow = sheet.createRow((short) 1);
			for (int columnIndex=0;columnIndex< key.length;columnIndex++) {
				HSSFCell cell = keyRow.createCell(columnIndex);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(new HSSFRichTextString(key[columnIndex]));
				
				HSSFCell cell2 = descRow.createCell(columnIndex);
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(new HSSFRichTextString(desc[columnIndex]));
			}			
			
			for (int i = 0; i < list.size(); i++) {
				HashMap map = (HashMap) list.get(i);
				
				HSSFRow row = sheet.createRow((short) i+2);
				for (int columnIndex=0;columnIndex< key.length;columnIndex++) {
					HSSFCell cell = row.createCell(columnIndex);
					
					Object value = map.get(key[columnIndex]);
					
					if (value== null) value="";
					
					if(value instanceof Double || value instanceof Integer) {
						double doubleValue=Double.parseDouble(value.toString());
						cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
						cell.setCellValue(doubleValue);
					} else if(value instanceof String) {
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(new HSSFRichTextString(value.toString()));
					} else if(value instanceof Date) {
						String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(new HSSFRichTextString(date));
					}
				}
			}
			fOut = new FileOutputStream(path
					+ fileName);
			workbook.write(fOut);
			fOut.flush();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
				try {
					fOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
